<?php

namespace common\models\c2\entity;

use backend\components\behaviors\RegionBehavior;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%region}}".
 *
 * @property integer $id
 * @property string $name
 * @property integer $parent_id
 * @property integer $level
 */
class Region extends \cza\base\models\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%region}}';
    }

    // public function behaviors()
    // {
    //     return array_merge(parent::behaviors(), RegionBehavior::className()); // TODO: Change the autogenerated stub
    // }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'name', 'parent_id', 'level'], 'required'],
            [['id', 'parent_id', 'level'], 'integer'],
            [['name'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'name' => Yii::t('app.c2', 'Name'),
            'parent_id' => Yii::t('app.c2', 'Parent ID'),
            'level' => Yii::t('app.c2', 'Level'),
        ];
    }

    /**
     * @inheritdoc
     * @return \common\models\c2\query\RegionQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \common\models\c2\query\RegionQuery(get_called_class());
    }

    /**
     * setup default values
     **/
    public function loadDefaultValues($skipIfSet = true)
    {
        parent::loadDefaultValues($skipIfSet);
    }

    public static function getRegion($parentId = 0)
    {
        $result = static::find()->where(['parent_id' => $parentId])->asArray()->all();
        return ArrayHelper::map($result, 'id', 'name');
    }

}
